home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / CENVIW2.ZIP / CMM_VS_C.DOC < prev    next >
Text File  |  1993-09-13  |  21KB  |  488 lines

  1.                     CEnvi Shareware Manual, Chapter 3:
  2.                      Cmm versus C, for C Programmers
  3.  
  4.  
  5.                      CEnvi unregistered version 1.003
  6.                              13 September 1993
  7.  
  8.  
  9.                        CEnvi Shareware User's Manual
  10.           Copyright 1993, Nombas, All Rights Reserved.
  11.           Published by Nombas, P.O. Box 875, Medford, MA 02155 USA
  12.           (617)391-6595
  13.  
  14.  
  15.           Thank you for trying this shareware version of CEnvi from Nombas.
  16.  
  17. 3. Cmm versus C: The Cmm language for C programmers
  18.  
  19.           This chapter is for those who already know how to program in the
  20.           C language.  This chapter describes only those elements of Cmm          that differ from standard C, and so if you don't already
  21.           understand C, then this shouldn't have much meaning for you.
  22.  
  23.           Non-C programmers should instead look at the previous chapter.
  24.  
  25.           Since it is assumed that readers of this chapter are already
  26.           knowledgeable in C, only those aspects of Cmm that differ from C
  27.           are described here.  If it's not mentioned here, then assume that
  28.           Cmm behavior will be standard C.
  29.  
  30.           Deviations from C are a result of these two harmonious Cmm
  31.           directives: Convenience and Safety.  Cmm is different from C
  32.           where the change makes Cmm more convenient for small programs,
  33.           command-line code, or scripting files, or if unaltered C rules
  34.           encourage coding that is potentially unsafe.
  35.  
  36. 3.1  .   C Minus Minus
  37.  
  38.           Cmm is "C minus minus" where the minuses are Type Declarations
  39.           and Pointers.  If you already know C and can remember to forget
  40.           these two aspects of C (I repeat, no Type Declarations and no
  41.           Pointers) then you know Cmm.  If you were to take C code, and
  42.           delete all the lines, code-words, and symbols that either declare
  43.           data types or explicitly point to data, then you would be left
  44.           with Cmm code; and although you would be removing bytes of source
  45.           code, you would not be removing capabilities.
  46.  
  47.           All of the details below that compare Cmm against C follow from
  48.           the general rule:
  49.            *Cmm is C minus Type Declarations and minus Pointers.
  50.  
  51. 3.2  .   Data Types
  52.  
  53.           The only recognized data types are Float, Long, and Byte.  The
  54.           words "Float", "Long", and "Byte" do not appear in Cmm source
  55.           code; instead, the data types are determined by context.  For
  56.           instance 6 is a Long, 6.6 is a Float, and '6' is a Byte.  Byte is
  57.           unsigned, and the other types are signed.
  58.  
  59. 3.3  .   Automatic Type Declaration
  60.  
  61.           There are no type declarators and no type casting.  Types are
  62.           determined from context.  If the code says "i=6" then i is a
  63.           Long, unless a previous statement has indicated otherwise.
  64.  
  65.           For instance, this C code:
  66.               int Max(int a,int b)
  67.               {
  68.                 int result;
  69.                 result = ( a < b ) ? b : a ;
  70.                 return result;
  71.               }
  72.           could become this Cmm code:
  73.               Max(a,b)
  74.               {
  75.                 result = ( a < b ) ? b : a ;
  76.                 return result;
  77.               }
  78.  
  79. 3.4  .   Array Representation
  80.  
  81.           Arrays are used in Cmm much like they are in C, except that they
  82.           are stored differently: a first-order array (e.g., a string) is
  83.           stored in consecutive bytes in memory, but arrays of arrays are
  84.           not in consecutive memory locations.  The C declaration "char
  85.           c[3][3];" would state that there are sixteen consecutive bytes in
  86.           memory.  In Cmm a similar statement such as "c[2][2] = 'A'" would
  87.           tell you that there are (at least) three arrays of characters,
  88.           and the third array of arrays has (at least) three characters in
  89.           it; and although the characters in c[0] are in consecutive bytes,
  90.           and the characters in c[1] are in consecutive bytes, the two
  91.           arrays c[0] and c[1] are not necessarily adjacent in memory.
  92.  
  93. 3.4  .1    Array Arithmetic
  94.  
  95.           When one array is assigned to the other, as in:
  96.               foo = "cat";
  97.               goo = foo;
  98.           then both variables define the same array and start at the same
  99.           offset 0.  In this case, if foo[2] is changed then you will find
  100.           that goo[2] has also been changed.
  101.  
  102.           Integer addition and subtraction can also be performed on arrays.
  103.           Array addition or subtraction sets where the array is based.  By
  104.           altering the previous code segment to:
  105.               foo = "cat";
  106.               goo = foo + 1;
  107.           goo and foo would now be arrays containing the same data, except
  108.           that now goo is based one element further, and foo[2] is now the
  109.           same data as goo[1].
  110.  
  111.           To demonstrate:
  112.               foo = "cat";  // foo[0] is 'c', foo[1] = 'a'
  113.               goo = foo + 1;// goo[0] is 'a', goo[-1] = 'c'
  114.               goo[0] = 'u'; // goo[0] is 'u', foo[1] = 'u', foo is "cut"
  115.               goo++;        // goo[0] is 't', goo[-2] = 'c'
  116.               goo[-2] = 'b' // goo[0] is 't', foo[0] = 'b', foo is "but"
  117.  
  118. 3.4  .2    Automatic Array Allocation
  119.  
  120.           Arrays are dynamic, and any index, (positive or negative) into an
  121.           array is always valid.  If an element of an array is referred to,
  122.           then the Cmm must see to it that such an element will exist.  For
  123.           instance if the first statement in the Cmm source code is "foo[4]
  124.           = 7;" then the Cmm interpreter will make an array of 5 integers
  125.           referred to by the variable foo.  If a statement further on
  126.           refers to "foo[6]" then the Cmm interpreter will grow foo, if it
  127.           has to, to ensure that the element foo[6] exists.  This works
  128.           with negative indices as well.  When you refer to foo[-10], then
  129.           foo is grown in the other direction if it needs to be, but foo[4]
  130.           will still refer to that "7" you put there earlier.  Arrays can
  131.           reach any dimension order, so that foo[6][7][34][-1][4] is a
  132.           valid value.
  133.  
  134. 3.5  .   Structures
  135.  
  136.           Structures are created dynamically, and their elements are not
  137.           necessarily contiguous in memory.  When CEnvi comes across the
  138.           statement 'foo.animal = "dog"' it creates a structure element of
  139.           foo that is referred to as "animal" and is an array of
  140.           characters, and this "animal" variable is thereafter carried
  141.           around with "foo" (much like a stem variable in REXX).  The
  142.           resulting code looks very much like regular C code, except that
  143.           there is not a separate structure definition anywhere.
  144.  
  145.           This C code:
  146.  
  147.               struct Point {
  148.                 int Row;
  149.                 int Column;
  150.               };
  151.  
  152.               struct Square {
  153.                 struct Point BottomLeft;
  154.                 struct Point TopRight;
  155.               };
  156.  
  157.               void main()
  158.               {
  159.                 struct Square sq;
  160.                 int Area;
  161.                 sq.BottomLeft.Row = 1;
  162.                 sq.BottomLeft.Column = 15;
  163.                 sq.TopRight.Row = 82;
  164.                 sq.TopRight.Column = 120;
  165.                 Area = AreaOfASquare(sq);
  166.               }
  167.  
  168.               int AreaOfASquare(struct Square s)
  169.               {
  170.                 int width, height;
  171.                 width = s.TopRight.Column - s.BottomLeft.Column + 1;
  172.                 height = s.TopRight.Row - s.BottomLeft.Row + 1;
  173.                 return( length * height );
  174.               }
  175.  
  176.           can be changed into the equivalent Cmm code simply be removing
  177.           declaration lines, resulting in:
  178.  
  179.               main()
  180.               {
  181.                 sq.BottomLeft.Row = 1;
  182.                 sq.BottomLeft.Column = 15;
  183.                 sq.TopRight.Row = 82;
  184.                 sq.TopRight.Column = 120;
  185.                 Area = AreaOfASquare(sq);